<!--
  * 产品
  *
  * @Author:    lee
  * @Date:      2022-12-13 18:28:03
  * @Copyright  lee
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">

      <a-form-item label="组织部门" class="smart-query-form-item">
        <a-input style="width: 150px" v-model:value="queryForm.code" placeholder="组织部门" />
      </a-form-item>



      <a-form-item class="smart-query-form-item">
        <a-button type="primary" @click="queryData">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery" class="smart-margin-left10">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
        <a-button @click="report" class="smart-margin-left10">
          <template #icon>
            <DownloadOutlined />
          </template>
          导出
        </a-button>


      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block"></div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns"  :refresh="queryData" />
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格 begin ----------->
    <a-table
        size="small"
        :dataSource="tableData"
        :columns="columns"
        rowKey="id"
        bordered
        :loading="tableLoading"
        :pagination="false"
        @change="handleTableChange"
    >
      <!--      :row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }"-->
      <template #bodyCell="{ text, record, column }">
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button type="link">编辑</a-button>
          </div>
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
          showSizeChanger
          showQuickJumper
          show-less-items
          :pageSizeOptions="PAGE_SIZE_OPTIONS"
          :defaultPageSize="queryForm.pageSize"
          v-model:current="queryForm.pageNum"
          v-model:pageSize="queryForm.pageSize"
          :total="total"
          @change="queryData"
          @showSizeChange="queryData"
          :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>
</template>
<script setup>
import { reactive, ref, onMounted, watch } from 'vue';
import { message, Modal } from 'ant-design-vue';
import { SmartLoading } from '/@/components/framework/smart-loading';
import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
import { smartSentry } from '/@/lib/smart-sentry';
import TableOperator from '/@/components/support/table-operator/index.vue';
import { defaultTimeRanges } from '/@/lib/default-time-ranges';
import { useRoute, useRouter } from 'vue-router';
import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';

// ---------------------------- 表格列 ----------------------------
const route = useRoute();
const props = defineProps({
  activeKey: {
    type: String,
    default: '2',
  },
});
//待分配、处理中、已完成、已驳回、已终止
const statusList = ref(['待分配','处理中','已完成','已驳回','已终止']);
const typeList = ref(['报销单工单','付款单工单','费用申请工单','异常申诉工单']);
const firstList = ref([]);

const columns = ref([

  {
    title: '上级组织编号',
    dataIndex: 'parentDepartmentCode',
    ellipsis: true,
  },
  {
    title: '上级组织名称',
    dataIndex: 'parentDepartmentName',
    ellipsis: true,
  },
  {
    title: '部门编号',
    dataIndex: 'code',
    ellipsis: true,
  },
  {
    title: '部门名称',
    dataIndex: 'name',
    ellipsis: true,
  },

  {
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
    width: 90,
  },
]);

// ---------------------------- 查询数据表单和方法 ----------------------------

const queryFormState = {
  biCode: undefined, //背调订单编号
  name: undefined, //候选人姓名
  idCard: undefined, //候选人证件号码
  orgId: undefined, //拟应聘公司
  departmentId: undefined, //组织架构
  code: undefined, //背调工单编号
  handlerName: route.query.handlerName, //处理人
  contacts: undefined, //联系人
  contactsPhone: undefined, //联系人电话号码
  createTime: [], //创建时间
  createTimeBegin: undefined, //创建时间 开始
  createTimeEnd: undefined, //创建时间 结束
  pageNum: 1,
  pageSize: 10,
};
// 查询表单form
const queryForm = reactive({ ...queryFormState });
// 表格加载loading
const tableLoading = ref(false);
// 表格数据
const tableData = ref([]);
// 总数
const total = ref(0);

// 重置查询条件
function resetQuery() {
  let pageSize = queryForm.pageSize;
  Object.assign(queryForm, queryFormState);
  queryForm.pageSize = pageSize;
  departmentValue.value = undefined;
  queryData();
}

// 查询数据
async function queryData() {
  tableLoading.value = true;
  try {
    if (route.query.queryForm != '[object Object]') {
      if (typeof route.query.queryForm != 'undefined') {
        if (route.query.queryForm != JSON.stringify(queryForm)) {
          Object.assign(queryForm, JSON.parse(route.query.queryForm));
          delete route.query.queryForm;
        }
      }
    }
    let queryResult = {
      "code": 0,
      "level": null,
      "msg": "success",
      "ok": true,
      "data": {
        "pageNum": 1,
        "pageSize": 10,
        "total": 56235,
        "pages": 5624,
        "list": [
          {
            "id": 1,
            "orgId": null,
            "departmentId": "62734ef96b691f6782568ce2",
            "createdAt": "2020-12-01 13:01:42",
            "updatedAt": "2025-09-08 09:58:29",
            "openDepartmenId": null,
            "name": "人力综合部",
            "description": null,
            "parentDepartmentId": "62734ef97fb84265f273a998",
            "parentDepartmentName": "俄罗斯事业部",
            "code": "50908113",
            "memberCount": null,
            "hasChildren": null,
            "isVirtualNode": null,
            "createTime": "2023-03-25 21:56:38",
            "organizationCode": null,
            "organizationName": null,
            "city": null,
            "cityValue": null,
            "bg": null,
            "contractSubjectId": null,
            "subject": null,
            "sourceType": "ehr",
            "parentDepartmentCode": "50907949"
          },
          {
            "id": 2,
            "orgId": null,
            "departmentId": "62734ef93a2c664a0d714cbb",
            "createdAt": "2020-12-01 13:01:42",
            "updatedAt": "2025-09-08 09:58:27",
            "openDepartmenId": null,
            "name": "营销品牌与科技赋能",
            "description": null,
            "parentDepartmentId": "62734ef9ad5736cc4fea528f",
            "parentDepartmentName": "酒店BU事业部本部",
            "code": "50928615",
            "memberCount": null,
            "hasChildren": null,
            "isVirtualNode": null,
            "createTime": "2023-03-25 21:56:38",
            "organizationCode": null,
            "organizationName": null,
            "city": null,
            "cityValue": null,
            "bg": null,
            "contractSubjectId": null,
            "subject": null,
            "sourceType": "ehr",
            "parentDepartmentCode": "50925525"
          },
          {
            "id": 3,
            "orgId": null,
            "departmentId": "62683a6702b4bdac00a99357",
            "createdAt": "2020-12-01 13:01:42",
            "updatedAt": "2020-12-01 13:01:42",
            "openDepartmenId": null,
            "name": "松花湖",
            "description": null,
            "parentDepartmentId": "62683a6792124837c7dc315d",
            "parentDepartmentName": null,
            "code": "50831102",
            "memberCount": null,
            "hasChildren": null,
            "isVirtualNode": null,
            "createTime": "2023-03-25 21:56:38",
            "organizationCode": null,
            "organizationName": null,
            "city": null,
            "cityValue": null,
            "bg": null,
            "contractSubjectId": null,
            "subject": null,
            "sourceType": "ehr",
            "parentDepartmentCode": null
          },
          {
            "id": 4,
            "orgId": null,
            "departmentId": "62734ef96ed696ae27116c16",
            "createdAt": "2020-12-01 13:01:42",
            "updatedAt": "2025-09-08 09:58:30",
            "openDepartmenId": null,
            "name": "王子酒店",
            "description": null,
            "parentDepartmentId": "62734ef98b199f5aedc7e433",
            "parentDepartmentName": "酒店",
            "code": "50925660",
            "memberCount": null,
            "hasChildren": null,
            "isVirtualNode": null,
            "createTime": "2023-03-25 21:56:38",
            "organizationCode": null,
            "organizationName": null,
            "city": null,
            "cityValue": null,
            "bg": null,
            "contractSubjectId": null,
            "subject": null,
            "sourceType": "ehr",
            "parentDepartmentCode": "50925645"
          },
          {
            "id": 5,
            "orgId": null,
            "departmentId": "62734ef960841b5f4c874b33",
            "createdAt": "2020-12-01 13:01:42",
            "updatedAt": "2025-09-08 09:58:29",
            "openDepartmenId": null,
            "name": "滑雪场",
            "description": null,
            "parentDepartmentId": "62734ef967decd21ae3d123e",
            "parentDepartmentName": "松花湖",
            "code": "50925654",
            "memberCount": null,
            "hasChildren": null,
            "isVirtualNode": null,
            "createTime": "2023-03-25 21:56:38",
            "organizationCode": null,
            "organizationName": null,
            "city": null,
            "cityValue": null,
            "bg": null,
            "contractSubjectId": null,
            "subject": null,
            "sourceType": "ehr",
            "parentDepartmentCode": "50925599"
          },
          {
            "id": 6,
            "orgId": null,
            "departmentId": "62734ef9c19e55339ac1cb9e",
            "createdAt": "2020-12-01 13:01:42",
            "updatedAt": "2025-09-08 09:58:33",
            "openDepartmenId": null,
            "name": "西餐厅",
            "description": null,
            "parentDepartmentId": "62734ef9b1121fff237b4a68",
            "parentDepartmentName": "餐饮部",
            "code": "50925758",
            "memberCount": null,
            "hasChildren": null,
            "isVirtualNode": null,
            "createTime": "2023-03-25 21:56:38",
            "organizationCode": null,
            "organizationName": null,
            "city": null,
            "cityValue": null,
            "bg": null,
            "contractSubjectId": null,
            "subject": null,
            "sourceType": "ehr",
            "parentDepartmentCode": "50925716"
          },
          {
            "id": 7,
            "orgId": null,
            "departmentId": "62734efab109c24fc1ee6265",
            "createdAt": "2020-12-01 13:01:42",
            "updatedAt": "2025-09-08 09:59:12",
            "openDepartmenId": null,
            "name": "三亚酒店",
            "description": null,
            "parentDepartmentId": "64d348e932b502714d81e439",
            "parentDepartmentName": "轻资产酒店",
            "code": "50941946",
            "memberCount": null,
            "hasChildren": null,
            "isVirtualNode": null,
            "createTime": "2023-03-25 21:56:38",
            "organizationCode": null,
            "organizationName": null,
            "city": null,
            "cityValue": null,
            "bg": null,
            "contractSubjectId": null,
            "subject": null,
            "sourceType": "ehr",
            "parentDepartmentCode": "50959320"
          },
          {
            "id": 8,
            "orgId": null,
            "departmentId": "62734efa728efb5e1259c650",
            "createdAt": "2020-12-01 13:01:42",
            "updatedAt": "2025-09-08 09:58:59",
            "openDepartmenId": null,
            "name": "项目管理部",
            "description": null,
            "parentDepartmentId": "62734efab4eafffd7669e179",
            "parentDepartmentName": "小海陀",
            "code": "50925567",
            "memberCount": null,
            "hasChildren": null,
            "isVirtualNode": null,
            "createTime": "2023-03-25 21:56:38",
            "organizationCode": null,
            "organizationName": null,
            "city": null,
            "cityValue": null,
            "bg": null,
            "contractSubjectId": null,
            "subject": null,
            "sourceType": "ehr",
            "parentDepartmentCode": "50925627"
          },
          {
            "id": 9,
            "orgId": null,
            "departmentId": "62734efaf06274192d48833f",
            "createdAt": "2020-12-01 13:01:42",
            "updatedAt": "2025-09-08 09:58:26",
            "openDepartmenId": null,
            "name": "餐饮组",
            "description": null,
            "parentDepartmentId": "62734efac5d9d156329f497d",
            "parentDepartmentName": "运营部",
            "code": "50930054",
            "memberCount": null,
            "hasChildren": null,
            "isVirtualNode": null,
            "createTime": "2023-03-25 21:56:38",
            "organizationCode": null,
            "organizationName": null,
            "city": null,
            "cityValue": null,
            "bg": null,
            "contractSubjectId": null,
            "subject": null,
            "sourceType": "ehr",
            "parentDepartmentCode": "50929772"
          },
          {
            "id": 10,
            "orgId": null,
            "departmentId": "62734ef938f19687075402be",
            "createdAt": "2020-12-01 13:01:42",
            "updatedAt": "2025-09-08 09:58:27",
            "openDepartmenId": null,
            "name": "产品设计中心",
            "description": null,
            "parentDepartmentId": "62734ef92c1e1438e60d4426",
            "parentDepartmentName": "北京办公室",
            "code": "50925546",
            "memberCount": null,
            "hasChildren": null,
            "isVirtualNode": null,
            "createTime": "2023-03-25 21:56:38",
            "organizationCode": null,
            "organizationName": null,
            "city": null,
            "cityValue": null,
            "bg": null,
            "contractSubjectId": null,
            "subject": null,
            "sourceType": "ehr",
            "parentDepartmentCode": "50925597"
          }
        ],
        "emptyFlag": false
      }
    } ;

    tableData.value = queryResult.data.list;
    total.value = queryResult.data.total;
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    tableLoading.value = false;
  }
}
function handleTableChange(pagination, filters, sorter, currentDataSource) {
  const sortItem = setSorter(sorter);
  queryForm.sortItemList = [];
  queryForm.sortItemList.push(sortItem);
  queryData();
}

function setSorter(sorter) {
  let re = 'ascend' === sorter.order;

  const sortItem = { column: toLine(sorter.field), isAsc: re };

  return sortItem;
}

function toLine(name) {
  return name.replace(/([A-Z])/g, '_$1').toLowerCase();
}
function onChangeCreateTime(dates, dateStrings) {
  queryForm.createTimeBegin = dateStrings[0];
  queryForm.createTimeEnd = dateStrings[1];
}

onMounted(() => {
  queryData();
});

// ---------------------------- 添加/修改 ----------------------------
const formRef = ref();

function showForm(data) {
  formRef.value.show(data);
}

// 选择表格行
const selectedRowKeyList = ref([]);

function onSelectChange(selectedRowKeys) {
  selectedRowKeyList.value = selectedRowKeys;
}

// 进入详情
const router = useRouter();
function toDetail(data) {
  router.push({
    path: '/hrssc/ticket/ticket-detail',
    query: {
      ticketTaskId: data.id,
      id: data.biId,
      backPath: '/hrssc/ticket/exception-list',
      queryForm: JSON.stringify(queryForm),
      TabactiveKey: props.activeKey,
    },
  });
}

const treeData = ref();
const departmentValue = ref();
const departmentSearchValue = ref('');


// 导出数据
async function report() {
  try {
  //  await ticketApi.reportExceptionData(queryForm);
  } catch (e) {
    smartSentry.captureError(e);
  }
}
</script>
